extends Control
var tcp_link :StreamPeerTCP
var xxx :Thread
var time_out :bool
var online :bool
signal xxx_done



func  _ready():
	tcp_link = StreamPeerTCP.new()
	xxx_done.connect(close_xxx)     #线程的信号连接到销毁方法
	xxx = Thread.new()


func _process(delta):
	match tcp_link.get_status():
		2:
			if tcp_link.get_available_bytes() > 2:    #检索此连接的数据流
				var recive_mess = tcp_link.get_var()
				if typeof(recive_mess) == TYPE_ARRAY: #在这里提前接收登录服务器发来的消息，
					print(recive_mess)                #如果消息是数组，就会被拦截下来赋值给game节点的
					$"../game".login_recive_data = recive_mess # login_recive_data变量，包含开始游戏人物数据
				else :
					$Label.text = recive_mess
					_mother_fucker_maching(recive_mess)
				
	
			
func _mother_fucker_maching(recive_mess:String): #数据流处理函数
	if recive_mess == "" :
		return
	else :
		var code = recive_mess.get_slice(",",0)
		var acc = recive_mess.get_slice(",",1)
		var pss = recive_mess.get_slice(",",2)
		var nam = recive_mess.get_slice(",",3)
		match code:
			"p":  
				$zhuce.visible = true
				$messbox.messbox("账号重复！")
			"v":
				$messbox.messbox("注册成功\n账号：%s\n密码：%s" %[acc,pss])
			"n":
				$messbox.messbox("账号不存在！")
				_tween_animation(true)
			"x":
				$messbox.messbox("密码错误！")
				_tween_animation(true)
			"s":#<<<<<<<<<------------------刷新角色选择界面-------
				$toy.visible = true
				$toy.display_player(acc,pss,nam)
			"f":
				$messbox.messbox("角色栏已满！")
			
			

func _tween_animation(up :bool) -> Signal:  #过渡动画，并返回完成信号
	var tween = create_tween()
	if up:
		tween.tween_property($login,"position",Vector2(430,130),0.3).set_trans(Tween.TRANS_CIRC)
		tween.tween_property($login,"position",Vector2(430,150),0.3)
		return tween.finished
	else :
		tween.tween_property($login,"position",Vector2(430,100),0.3)
		tween.tween_property($login,"position",Vector2(430,660),0.3).set_ease(Tween.EASE_OUT)
		return tween.finished
	



#--------------------------------------连接服务器按钮按下------------------------
func _on_button_pressed():
	if xxx.is_started():
		return
	else :
		xxx.start(log_to_server)
		time_out = false
		$Timer.start(5)              #启动或者重置计时，时间5s
		$Label.text = "正在连接服务器..."
		$TextureRect/VBoxContainer/Button.disabled = true
#--------------------------------------登录游戏按钮按下-------------------------
func _on_button_3_pressed():
	_tween_animation(true) # |滑上来动画

func _on_ok_login():
	var acc = $login/acc.text
	var code = $login/code.text
	await _tween_animation(false)
	tcp_link.put_var("a," + acc + "," + code)#---------->>>>登录键按下向服务器发送登录数据

func _on_no_login():
	_tween_animation(false) # |下去的动画
#-------------------------------------注册账号按钮按下----------------------------
func _on_button_2_pressed():
	$zhuce.visible = true

func _on_ok_pressed():
	var acc :String= $zhuce/acc.text
	var code :String= $zhuce/code.text
	if acc.length() > 5 and code.length() > 5:
		tcp_link.put_var("r," + acc + "," + code) #------->>>>简单判断之后，向服务器发送注册数据
		$zhuce.visible = false
	else :
		$messbox.messbox("帐户或密码长度太短！")

func _on_no_zhuce():
	$zhuce.visible = false
	

#--------------------------------------《线程》连接服务器方法---------------------------
func log_to_server():
	tcp_link.connect_to_host("127.0.0.1",9527)        
	while tcp_link.get_status() != 2:
		tcp_link.poll()
		if tcp_link.get_status() == 2:
				tcp_link.put_var("有客户端连接...")
				online = true
				call_deferred("emit_signal","xxx_done")     #线程循环走完后发出信号
				print("已连接")
				break
		elif time_out:
			tcp_link.disconnect_from_host()
			call_deferred("emit_signal","xxx_done")          #线程循环走完后发出信号
			online = false
			print("断开1")
			break

#---------------------------连接成功关闭线程--------------------------------------
func close_xxx():             #信号触发此方法
	print("关闭线程" + xxx.get_id())
	xxx.wait_to_finish()
	if online:
		$TextureRect/VBoxContainer/Button2.disabled = false
		$TextureRect/VBoxContainer/Button3.disabled = false

#---------------------------连接超时关闭线程---------------------------------------
func _on_timer_timeout(): #此计时器不是一次性的，每5秒检查一次连接状态，按下连接服务器按钮重置时间，5秒后触发超时
	tcp_link.poll()
	if tcp_link.get_status() == 2:
		return
	else :
		time_out = true
		$Label.text = "连接服务器超时..."
		$TextureRect/VBoxContainer/Button.disabled = false
		$TextureRect/VBoxContainer/Button2.disabled = true
		$TextureRect/VBoxContainer/Button3.disabled = true
func _exit_tree():
	if xxx.is_started():
		xxx.wait_to_finish()
